package org.nf.trading.service.app.message.websocket;


import jakarta.websocket.HandshakeResponse;
import jakarta.websocket.server.HandshakeRequest;
import jakarta.websocket.server.ServerEndpointConfig;
import jakarta.websocket.server.ServerEndpointConfig.Configurator;
import org.nf.trading.util.JwtUtils;

import javax.print.DocFlavor;
import java.util.Arrays;

public class HandShakeConfigurator extends Configurator {
    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        String token = request.getHeaders().get("sec-websocket-protocol").get(0);
        System.out.println("token: ===>"+token);
        Integer uid = JwtUtils.getPayload(token, "uid", Integer.class);
        //再将用户信息保存到websocket的session的属性中
        sec.getUserProperties().put("userId", String.valueOf(uid));
        response.getHeaders().put("sec-websocket-protocol", Arrays.asList(token));
    }
}
